﻿using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.Web.Controllers
{
    public class ZhiFu1Controller : Controller
    {
        // GET: ZhiFu1
        public ActionResult Index(int id)
        {
            Order order =  OrderService.SelectId(id);

            DefaultAopClient client = new DefaultAopClient(config.gatewayUrl, config.app_id, config.private_key, "json", "1.0", config.sign_type, config.alipay_public_key, config.charset, false);

            // 外部订单号，商户网站订单系统中唯一的订单号
            string OrderId = order.OrderId;
            // 订单名称
            string _ProductName = order.ProductName;

            // 付款金额
            string total_amount = order.Price.ToString();
            //string _CustId = CustId;
            //string _ProductPrth = ProductPrth;
            //string _IsTake = IsTake;
            //string _Count = Count; 
            //string _State = State;
            //string _OrderTime = OrderTime;
            //string _IsFah = Price;

            // 商品描述
            string body = "10";

            OrderService.UpdateIsTake(OrderId);


            // 组装业务参数model
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.Body = body;
            model.Subject = _ProductName;
            model.TotalAmount = total_amount;
            model.OutTradeNo = OrderId;
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";

            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 设置同步回调地址
            request.SetReturnUrl("http://localhost:64132/Order/Index?OrderId=" + OrderId);
            // 设置异步通知接收地址
            request.SetNotifyUrl("http://localhost:64132/Order/Index");
            // 将业务model载入到request
            request.SetBizModel(model);



            AlipayTradePagePayResponse response = null;
            try
            {
                response = client.pageExecute(request, null, "post");
                Response.Write(response.Body);

            }
            catch (Exception exp)
            {
                throw exp;
            }

            return Content("");

        }
    }
    
}